#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

typedef int Position;
typedef int Tag;
typedef struct SNode *PtrToSNode;
struct SNode {
    double *data;
    Position Top1;
    Position Top2;
    int MaxSize;
};
typedef PtrToSNode Stack;

Stack CreateStack(int MaxSize)
{
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->data = (double *)malloc(sizeof(double) * MaxSize);
    S->Top1 = -1;
    S->Top2 = MaxSize;
    S->MaxSize = MaxSize;

    return S;
}

bool IsFull(Stack S)
{
    return (S->Top1 + 1 == S->Top2);
}

bool Push(Stack S, double X, Tag Stack_Index)
{
    if (IsFull(S))
    {
        printf("Stack is full !\n");
        return false;
    }
    else
    {
        if (Stack_Index == 1)
        {
            S->data[++(S->Top1)] = X;
            printf("Push into Stack_1 Success !\n");
            return S;
        }
        else
        {
            S->data[--(S->Top2)] = X;
            printf("Push into Stack_2 Success !\n");
            return S;            
        }
    }
}


int main()
{
    int MaxSize = 5, Stack_Index;

    Stack example_stack = CreateStack(MaxSize);
    double number;

    for (;;)
    {
        printf("\nChoose the stack to input(1 or 2, 0 to finish) : ");
        scanf("%d", &Stack_Index);
        if (Stack_Index == 0)
            break;
        else if (Stack_Index == 1 || Stack_Index == 2)
        {
            for (;;)
            {
                printf("Enter the number to input(0 to finish) : ");
                scanf("%lf", &number);
                if (number == 0)
                    break;
                if(!Push(example_stack, number, Stack_Index))
                    break;
            }

            if (IsFull(example_stack))
                break;
        }
        else
            printf("Invaild Stack Index !\n");
        
        if (IsFull(example_stack))
            break;
    }

    printf("\nStack_1 have %d numbers, Stack_2 have %d numbers\n", example_stack->Top1 + 1, MaxSize - example_stack->Top2);
    printf("\nThe stack have %d numbers in all\n", MaxSize + example_stack->Top1 - example_stack->Top2 + 1);
    for (int index = 0; index < MaxSize; index++)
        printf("%lf ", example_stack->data[index]);
    printf("\n\n");
}

